﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolManagementSystem.Report._1._HocVien;
using CoreData;
using SchoolManagementSystem.Controls;
using DevExpress.XtraEditors.Controls;
namespace SchoolManagementSystem.Forms.BusinessForm
{
    public partial class frmReportStudent : DevExpress.XtraEditors.XtraForm
    {
        public frmReportStudent()
        {
            InitializeComponent();
            
            SetDefault();

        }

        void SetDefault()
        {
            radioGroup1.SelectedIndex = 0;
            dateEdit_from.DateTime = DateTime.Now.AddMonths(-6);
            dateEdit_to.DateTime = DateTime.Now;
            buttonEdit_Student.Enabled = false;
            LoadYear();
            LoadBirthfrom();
            LoadBirthto();
        }

        long ClassiD;
        long CourseiD;
        long BranchiD;
        long StatusiD;
        long ChaneliD;

        void LoadYear()
        {
            ComboBoxItemCollection coll = comboBoxEdit_Year.Properties.Items;
            coll.BeginUpdate();
            try
            {
                comboBoxEdit_Year.Properties.NullText = "[Rỗng]";
                List<int> Year = new List<int>();
                Year.Add(DateTime.Now.Year);
                for (int i = DateTime.Now.Year-1; i>=2010; i--)
                {
                    Year.Add(i);
                }
                comboBoxEdit_Year.Properties.Items.AddRange(Year);
            }
            finally
            {
                coll.EndUpdate();
            }
            comboBoxEdit_Year.SelectedIndex = 0;


        }
        void LoadBirthfrom()
        {
            ComboBoxItemCollection coll = birthfrom.Properties.Items;
            coll.BeginUpdate();
            try
            {
                birthfrom.Properties.NullText = "[Rỗng]";
                List<int> Year = new List<int>();
                Year.Add(DateTime.Now.Year);
                for (int i = DateTime.Now.Year - 1; i >= 1970; i--)
                {
                    Year.Add(i);
                }
                birthfrom.Properties.Items.AddRange(Year);
            }
            finally
            {
                coll.EndUpdate();
            }
            birthfrom.SelectedIndex = 0;


        }
        void LoadBirthto()
        {
            ComboBoxItemCollection coll = birthto.Properties.Items;
            coll.BeginUpdate();
            try
            {
                birthto.Properties.NullText = "[Rỗng]";
                List<int> Year = new List<int>();
                Year.Add(DateTime.Now.Year);
                for (int i = DateTime.Now.Year - 1; i >= 1970; i--)
                {
                    Year.Add(i);
                }
                birthto.Properties.Items.AddRange(Year);
            }
            finally
            {
                coll.EndUpdate();
            }
            birthto.SelectedIndex = 0;


        }
        void ShowReport()
        {
            if (radioGroup1.SelectedIndex == 0)
            {
                DanhSachTheoLop();
            }
            if (radioGroup1.SelectedIndex == 1)
            {
                DanhSachTheoKhoa();
            }
            if (radioGroup1.SelectedIndex == 2)
            {
                DanhSachTheoNganh();
            }
            if (radioGroup1.SelectedIndex == 6)
            {
                DanhSachTheoNhiuTieuChi();
            }
            if (radioGroup1.SelectedIndex == 5)
            {
                NhapHocRaTruongNganHan();
            }
            if(radioGroup1.SelectedIndex==7)
            {
                DanhSachCacLopChuaTotNghiep();
            }
            if (radioGroup1.SelectedIndex == 8)
            {
                DanhSachCacLopDaTotNghiep();
            }
        }

        void SelectBox()
        {
            if (radioGroup1.SelectedIndex == 0)
            {
                buttonEdit_Branch.Enabled = false;
                buttonEdit_Course.Enabled = false;
                buttonEdit_Student.Enabled = false;
                buttonEdit_Class.Enabled = true;
                dateEdit_from.Enabled = false;
                dateEdit_to.Enabled = false;
                buttonEdit_Status.Enabled = false;
                buttonEdit_Channel.Enabled = false;
                comboBoxEdit_Year.Enabled = false;
                birthfrom.Enabled = false;
                birthto.Enabled = false;
                birthfrom.EditValue = "";
                birthto.EditValue = "";
                buttonEdit_Branch.EditValue = "";
                BranchiD = 0;

                buttonEdit_Class.EditValue = "";
                ClassiD = 0;

                buttonEdit_Course.EditValue = "";
                CourseiD = 0;

                buttonEdit_Status.EditValue = "";
                StatusiD = 0;

                buttonEdit_Channel.EditValue = "";
                ChaneliD = 0;

            }
            if (radioGroup1.SelectedIndex == 1)
            {
                buttonEdit_Branch.Enabled = false;
                buttonEdit_Course.Enabled = true;
                buttonEdit_Student.Enabled = false;
                buttonEdit_Class.Enabled = false;
                dateEdit_from.Enabled = false;
                dateEdit_to.Enabled = false; buttonEdit_Status.Enabled = false;
                buttonEdit_Channel.Enabled = false;
                comboBoxEdit_Year.Enabled = false;
                birthfrom.Enabled = false;
                birthto.Enabled = false;
                birthfrom.EditValue = "";
                birthto.EditValue = "";
                buttonEdit_Branch.EditValue = "";
                BranchiD = 0;

                buttonEdit_Class.EditValue = "";
                ClassiD = 0;

                buttonEdit_Course.EditValue = "";
                CourseiD = 0;

                buttonEdit_Status.EditValue = "";
                StatusiD = 0;

                buttonEdit_Channel.EditValue = "";
                ChaneliD = 0;
            }

            if (radioGroup1.SelectedIndex == 2)
            {
                buttonEdit_Branch.Enabled = true;
                dateEdit_from.Enabled = true;
                dateEdit_to.Enabled = true;
                buttonEdit_Course.Enabled = false;
                buttonEdit_Student.Enabled = false;
                buttonEdit_Class.Enabled = false; buttonEdit_Status.Enabled = false;
                buttonEdit_Channel.Enabled = false;
                comboBoxEdit_Year.Enabled = false;
                buttonEdit_Branch.EditValue = "";
                BranchiD = 0;
                birthfrom.Enabled = false;
                birthto.Enabled = false;
                birthfrom.EditValue = "";
                birthto.EditValue = "";
                buttonEdit_Class.EditValue = "";
                ClassiD = 0;

                buttonEdit_Course.EditValue = "";
                CourseiD = 0;

                buttonEdit_Status.EditValue = "";
                StatusiD = 0;

                buttonEdit_Channel.EditValue = "";
                ChaneliD = 0;
            }
            if (radioGroup1.SelectedIndex == 6)
            {
                comboBoxEdit_Year.Enabled = false;
                buttonEdit_Branch.Enabled = true;
                dateEdit_from.Enabled = true;
                dateEdit_to.Enabled = true;
                buttonEdit_Course.Enabled = true;
                buttonEdit_Student.Enabled = false;
                buttonEdit_Class.Enabled = true; buttonEdit_Status.Enabled = true;
                buttonEdit_Channel.Enabled = true;
                buttonEdit_Branch.EditValue = "";
                BranchiD = 0;
                birthfrom.Enabled = true;
                birthto.Enabled = true;
                birthto.EditValue = "";
                birthfrom.EditValue = "";
                buttonEdit_Class.EditValue = "";
                ClassiD = 0;

                buttonEdit_Course.EditValue = "";
                CourseiD = 0;

                buttonEdit_Status.EditValue = "";
                StatusiD = 0;

                buttonEdit_Channel.EditValue = "";
                ChaneliD = 0;
            }
            if (radioGroup1.SelectedIndex == 5)
            {
                comboBoxEdit_Year.Enabled = true;
                buttonEdit_Branch.Enabled = false;
                dateEdit_from.Enabled = false;
                dateEdit_to.Enabled = false;
                buttonEdit_Course.Enabled = false;
                buttonEdit_Student.Enabled = false;
                buttonEdit_Class.Enabled = false; buttonEdit_Status.Enabled = false;
                buttonEdit_Channel.Enabled = false;
                buttonEdit_Branch.EditValue = "";
                BranchiD = 0;
                birthfrom.Enabled = false;
                birthto.Enabled = false;
                birthfrom.EditValue = "";
                birthto.EditValue = "";
                buttonEdit_Class.EditValue = "";
                ClassiD = 0;

                buttonEdit_Course.EditValue = "";
                CourseiD = 0;

                buttonEdit_Status.EditValue = "";
                StatusiD = 0;

                buttonEdit_Channel.EditValue = "";
                ChaneliD = 0;
            }

            if (radioGroup1.SelectedIndex == 7)
            {
                comboBoxEdit_Year.Enabled = false;
                buttonEdit_Branch.Enabled = false;
                dateEdit_from.Enabled = true;
                dateEdit_to.Enabled = true;
                buttonEdit_Course.Enabled = true;
                buttonEdit_Student.Enabled = false;
                buttonEdit_Class.Enabled = true; buttonEdit_Status.Enabled = false;
                buttonEdit_Channel.Enabled = false;
                buttonEdit_Branch.EditValue = "";
                BranchiD = 0;
                birthfrom.Enabled = false;
                birthto.Enabled = false;
                birthfrom.EditValue = "";
                birthto.EditValue = "";
                buttonEdit_Class.EditValue = "";
                ClassiD = 0;

                buttonEdit_Course.EditValue = "";
                CourseiD = 0;

                buttonEdit_Status.EditValue = "";
                StatusiD = 0;

                buttonEdit_Channel.EditValue = "";
                ChaneliD = 0;
            }
            if (radioGroup1.SelectedIndex == 8)
            {
                comboBoxEdit_Year.Enabled = false;
                buttonEdit_Branch.Enabled = false;
                dateEdit_from.Enabled = true;
                dateEdit_to.Enabled = true;
                buttonEdit_Course.Enabled = true;
                buttonEdit_Student.Enabled = false;
                buttonEdit_Class.Enabled = true; buttonEdit_Status.Enabled = false;
                buttonEdit_Channel.Enabled = false;
                buttonEdit_Branch.EditValue = "";
                BranchiD = 0;
                birthfrom.Enabled = false;
                birthto.Enabled = false;
                birthfrom.EditValue = "";
                birthto.EditValue = "";
                buttonEdit_Class.EditValue = "";
                ClassiD = 0;

                buttonEdit_Course.EditValue = "";
                CourseiD = 0;

                buttonEdit_Status.EditValue = "";
                StatusiD = 0;

                buttonEdit_Channel.EditValue = "";
                ChaneliD = 0;
            }
        }

        private void simpleButton_View_Click(object sender, EventArgs e)
        {
            ShowReport();
        }

        void DanhSachTheoLop()
        {           
            if (ClassiD == 0)
            {
                MessageControl.ShowMessage("Chưa chọn lớp", "Error", MessageControl.Icon.Error);
                return;
            }
            var Class = (Class)RegisterService.RegisterServices.ClassService.GetByID(ClassiD);
            DanhSachHocVienTheoLop ds = new DanhSachHocVienTheoLop();
            ds.ClassSize = Class.Size;
            ds.ClassName = Class.Name;
            ds.Teacher = Class.TeacherExt;
            var data = HocVienReportData.LoadStudentByClass(ClassiD);
            ds.DataSource = data;
            printControl1.PrintingSystem = ds.PrintingSystem;
            ds.CreateDocument();
            
        }

        void DanhSachTheoKhoa()
        {
            if (CourseiD == 0)
            {
                MessageControl.ShowMessage("Chưa chọn khóa", "Error", MessageControl.Icon.Error);
                return;
            }
            var course = (Course)RegisterService.RegisterServices.CourseService.GetByID(CourseiD);
            DanhSachHocVienTheoKhoa ds = new DanhSachHocVienTheoKhoa();
            ds.CourseName = course.Name;
            var data = HocVienReportData.LoadStudentByCourse(CourseiD);
            ds.DataSource = data;
            printControl1.PrintingSystem = ds.PrintingSystem;
            ds.CreateDocument();

        }

        void DanhSachTheoNganh()
        {
            if (BranchiD == 0)
            {
                MessageControl.ShowMessage("Chưa chọn nghành", "Error", MessageControl.Icon.Error);
                return;
            }
            DanhSachHocVienTheoNganh ds = new DanhSachHocVienTheoNganh();
            var data = HocVienReportData.LoadStudentByBranch(BranchiD, dateEdit_from.DateTime, dateEdit_to.DateTime);
            ds.from = dateEdit_from.DateTime;
            ds.to = dateEdit_to.DateTime;
            ds.DataSource = data;
            printControl1.PrintingSystem = ds.PrintingSystem;
            ds.CreateDocument();

        }


        void DanhSachTheoNhiuTieuChi()
        {

            DanhSachHocVienTuyChon ds = new DanhSachHocVienTuyChon();
            int _birthfrom = 0;
            int _birthto = 0;
            if (birthfrom.EditValue != null)
            {
                if (!string.IsNullOrEmpty(birthfrom.EditValue.ToString()))
                {
                    _birthfrom = int.Parse(birthfrom.EditValue.ToString());
                }
            }
            if (birthto.EditValue != null)
            {
                if (!string.IsNullOrEmpty(birthto.EditValue.ToString()))
                {
                    _birthto = int.Parse(birthto.EditValue.ToString());
                }
            }
            var data = HocVienReportData.LoadStudentByAllCondition(BranchiD,CourseiD,ClassiD,StatusiD, ChaneliD, dateEdit_from.DateTime, dateEdit_to.DateTime, _birthfrom, _birthto);
            ds.from = dateEdit_from.DateTime;
            ds.to = dateEdit_to.DateTime;
            ds.DataSource = data;
            printControl1.PrintingSystem = ds.PrintingSystem;
            ds.CreateDocument();

        }

        void NhapHocRaTruongNganHan()
        {

            TongKetSoLuongNhapHocVaRaTruong_NganHan ds = new TongKetSoLuongNhapHocVaRaTruong_NganHan();
            var data = HocVienReportData.LoadTongKetSoLNhapHocVaRaTruongNganHan(int.Parse(comboBoxEdit_Year.SelectedItem.ToString()));
            ds.Year = int.Parse(comboBoxEdit_Year.SelectedItem.ToString());
            ds.DataSource = data;
            printControl1.PrintingSystem = ds.PrintingSystem;
            ds.CreateDocument();

        }

        void DanhSachCacLopChuaTotNghiep()
        {
            string Search = "Class.Active='False' and ";
            if(CourseiD!=0)
            {
                Search += "Course.ID =" + CourseiD + " and ";
            }
            if(ClassiD!=0)
            {
                Search += "Class.ID =" + ClassiD + " and ";

            }
            Search += " Convert(varchar(11), Class.StartDate, 120) BETWEEN '" + dateEdit_from.DateTime.ToString("yyyy-MM-dd") + "' and '" + dateEdit_to.DateTime.ToString("yyyy-MM-dd") + "'";
            DanhSachLopChuaTN ds = new DanhSachLopChuaTN();
            var data = HocVienReportData.LoadReportClassNotDone(Search);
           
            ds.DataSource = data;
            printControl1.PrintingSystem = ds.PrintingSystem;
            ds.CreateDocument();
        }

        void DanhSachCacLopDaTotNghiep()
        {
            string Search = "Class.Active='True' and ";
            if (CourseiD != 0)
            {
                Search += "Course.ID =" + CourseiD + " and ";
            }
            if (ClassiD != 0)
            {
                Search += "Class.ID =" + ClassiD + " and ";

            }
            Search += " Convert(varchar(11), Class.StartDate, 120) BETWEEN '" + dateEdit_from.DateTime.ToString("yyyy-MM-dd") + "' and '" + dateEdit_to.DateTime.ToString("yyyy-MM-dd") + "'";
            DanhSachLopDaTN ds = new DanhSachLopDaTN();
            var data = HocVienReportData.LoadReportClassNotDone(Search);

            ds.DataSource = data;
            printControl1.PrintingSystem = ds.PrintingSystem;
            ds.CreateDocument();
        }

        private void buttonEdit_Class_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index == 0)
            {
                frmSelectClass selectclass = new frmSelectClass();
                if (selectclass.ShowDialog() == DialogResult.OK)
                {
                    string data = "[" + selectclass.Code + "] " + selectclass.Name;
                    buttonEdit_Class.EditValue = data;
                    ClassiD = selectclass.ID;
                }
                else {
                    buttonEdit_Class.EditValue = "" ;
                    ClassiD = 0;
                }
            }
        }

        private void radioGroup1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectBox();
        }

        private void buttonEdit_Course_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index == 0)
            {
                frmSelectCourse select = new frmSelectCourse();
                if (select.ShowDialog() == DialogResult.OK)
                {
                    string data = "[" + select.Code + "] " + select.Name;
                    buttonEdit_Course.EditValue = data;
                    CourseiD = select.ID;
                }
                else
                {
                    buttonEdit_Course.EditValue = ""; CourseiD = 0;
                }
            }
        }

        private void buttonEdit_Branch_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index == 0)
            {
                frmSelectBranch select = new frmSelectBranch();
                if (select.ShowDialog() == DialogResult.OK)
                {
                    string data = "[" + select.Code + "] " + select.Name;
                    buttonEdit_Branch.EditValue = data;
                    BranchiD = select.ID;
                }
                else
                {
                    buttonEdit_Branch.EditValue = ""; BranchiD = 0;
                }
            }
        }

        private void buttonEdit_Status_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index == 0)
            {
                frmSelectStatus select = new frmSelectStatus(1);
                if (select.ShowDialog() == DialogResult.OK)
                {
                    string data = "[" + select.Code + "] " + select.Name;
                    buttonEdit_Status.EditValue = data;
                    StatusiD = select.ID;
                }
                else
                {
                    buttonEdit_Status.EditValue = ""; StatusiD = 0;
                }
            }
        }

        private void buttonEdit_Channel_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index == 0)
            {
                frmSelectChannel select = new frmSelectChannel();
                if (select.ShowDialog() == DialogResult.OK)
                {
                    string data = "[" + select.Code + "] " + select.Name;
                    buttonEdit_Channel.EditValue = data;
                    ChaneliD = select.ID;
                }
                else
                {
                    buttonEdit_Channel.EditValue = ""; ChaneliD = 0;
                }
            }
        }

      

    }
}
